//
//  SPHomeCategoryView.m
//  ShopProject
//
//  Created by 颜魏 on 2018/11/27.
//  Copyright © 2018年 KB. All rights reserved.
//

#import "SPHomeCategoryView.h"
#import "SPProductCategoryModel.h"

@interface SPHomeCategoryView()

@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) UIScrollView    *sv;
@property (nonatomic, strong) UIView    *bgView;
@property (nonatomic, strong) UIView    *sepView;
@property (nonatomic, strong) NSMutableArray    *categoryButtons;

@end

@implementation SPHomeCategoryView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.categoryButtons = [[NSMutableArray alloc] init];
        self.selectIndex = 0;
        
        self.sv = [[UIScrollView alloc] initWithFrame:frame];
        self.sv.backgroundColor = [UIColor whiteColor];
        self.sv.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.sv];
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self.sv addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.offset(0);
            make.width.offset(self.bounds.size.width);
            make.height.offset(self.bounds.size.height);
        }];
    
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = RGBCOLOR(240, 240, 240);
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.offset(0);
            make.height.offset(0.8);
        }];
    }
    
    return self;
}

#pragma mark - get set
- (void)setCategorys:(NSArray *)categorys {
    _categorys = categorys;
    [self createCategoryButtons];
}

- (void)createCategoryButtons {
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.categoryButtons removeAllObjects];
    CGFloat left = 0;
    for (NSInteger i=0; i<self.categorys.count; i++) {
        left += 20;
        UIFont *font = [UIFont fontWithName:@"PingFangSC-Semibold" size:13];
        CGFloat width = [[self.categorys[i] categoryName] boundingRectWithSize:CGSizeMake(INT_MAX, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size.width;
        UIButton *btn = [[UIButton alloc] init];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:[self.categorys[i] categoryName] forState:UIControlStateNormal];
        [btn setTitleColor:RGBCOLOR(61, 61, 61) forState:UIControlStateSelected];
        [btn setTitleColor:RGBCOLOR(100, 100, 100) forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(actionOnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = font;
        btn.selected = i==self.selectIndex;
        [self.bgView addSubview:btn];
        [self.categoryButtons addObject:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.offset(0);
            make.left.offset(left);
            make.width.offset(width);
        }];
        left += width;
        if (i==self.selectIndex) {
            self.sepView = [[UIView alloc] init];
            self.sepView.backgroundColor = RGBCOLOR(61, 61, 61);
            [self.bgView addSubview:self.sepView];
            [self.sepView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.offset(0);
                make.left.equalTo(btn.mas_left);
                make.right.equalTo(btn.mas_right);
                make.height.offset(3);
            }];
        }
    }
    
    self.sv.contentSize = CGSizeMake(left+30, self.sv.bounds.size.height);
}

#pragma mark - action
- (void)actionOnClick:(UIButton *)button {
    self.selectIndex = [self.categoryButtons indexOfObject:button];
    self.selectIndex = self.selectIndex!=NSNotFound?self.selectIndex:0;
    for (NSInteger i=0; i<self.categoryButtons.count; i++) {
        UIButton *btn = self.categoryButtons[i];
        btn.selected = i==self.selectIndex;
        if (i==self.selectIndex) {
            [self.bgView bringSubviewToFront:self.sepView];
            [UIView animateWithDuration:0.3 animations:^{
                [self.sepView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(btn.mas_left);
                    make.right.equalTo(btn.mas_right);
                    make.bottom.offset(0);
                    make.height.offset(3);
                }];
                [self layoutIfNeeded];
            }];
        }
    }
    if (_onSelectCategory) {
        _onSelectCategory(self.categorys[self.selectIndex]);
    }
}


@end
